import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';

import {TeacherService} from "@train/teacher/teacher.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ActivatedRoute, Params, Router} from "@angular/router";

@Component({
  selector: 'test-select',
  templateUrl: './test-select.component.html',
  styleUrls: ['./test-select.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class TestSelectComponent implements OnInit {
  public dataStore = {
    testItemOptions: [],
    testItemIndex: 0,
    testItemId: 0,
    testContentOptions: [],
    testContentId: 0,
    testContentIndex: 0,
    testType: 0
  };
  public selected;

  constructor(private teacherService: TeacherService,
              private route: ActivatedRoute,
              private router: Router,
              private authService: AuthService) {
    this.route.params.switchMap((params: Params) => {
      return this.route.queryParams;
    }).subscribe((queryParams: Params) => {
      this.getTestCategory(queryParams['authOrgId']);
      this.authService.setAuthOrgId(queryParams['authOrgId']);
    });
  }

  /**
   * @description 处理父级选项（教练）
   * @author ouyqx-691
   * @date 2018-4-11
   */
  handleParentOption() {
    const testItemIndex = this.dataStore.testItemIndex;
    const testItemOption = this.dataStore.testItemOptions[testItemIndex];
    const testContentOptions = testItemOption.testItemList;
    this.dataStore.testContentOptions = testContentOptions;
    this.dataStore.testItemId = testItemOption.id;
    // this.dataStore.testType = testItemOption.testScoreType;
    this.handleChildOption();
  }

  /**
   * @description 处理子级选项（教练）
   * @author ouyqx-691
   * @date 2018-4-11
   */

  handleChildOption() {
    const {testContentOptions, testContentIndex} = this.dataStore;
    this.dataStore.testContentId = testContentOptions[testContentIndex].id;
    this.dataStore.testType = testContentOptions[testContentIndex].testScoreType;
  }

  goToTarget() {

    const {testItemId, testContentId, testType} = this.dataStore;
    this.router.navigate([`/train/teacher/classes`], {
      queryParams: {
        testItemId: testItemId,
        testContentId: testContentId,
        testType: testType
      }
    });
  }

  /**
   * @description 查询选择维度（教练）
   * @author ouyqx-691
   * @date 2018-4-11
   */
  getTestCategory(orgUserId) {
    const params = {
      orgUserId: orgUserId
    };
    this.teacherService.getTestCategory(params).subscribe((res) => {
      if (res.result === '0' && res.data) {
        this.dataStore.testItemOptions = res.data;
        this.handleParentOption(); // 默认选择第一项第一个
      }
    });
  }

  ngOnInit() {

  }

}
